package GUI;
/**hofit**/
import javax.net.ssl.SSLEngineResult.Status;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import Client.BusinessImplements;
import Controler.Member_Card_Controler;
import DAL.Member_DAL;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.border.LineBorder;

public class Update_Member_Card_Secondary extends Abstract_Jpanel {
	private final JLabel lblFirstName = new JLabel("First Name:");
	private final JLabel lblLastName = new JLabel("Last Name:");
	private final JLabel lblId = new JLabel("ID:");
	private final JLabel lblPhone = new JLabel("Phone:");
	private final JLabel lblEmail = new JLabel("Email:");
	private final JLabel lblStatus = new JLabel("Status:");
	private final JTextField txtName = new JTextField();
	private final JTextField txtLastName = new JTextField();
	private final JTextField txtID = new JTextField();
	private final JTextField txtPhone = new JTextField();
	private final JTextField txtEmail = new JTextField();
	private final JButton btnApply = new JButton("Apply");
	public JButton btnBack = new JButton("Back");
	private final JLabel lblUserDetailsUpdate = new JLabel("Update User Details ");
	private final JPanel panel = new JPanel();
	private final JPanel panel_1 = new JPanel();
	private final JComboBox comboBox = new JComboBox();
	private JTextField txtPassword;
	private final JPanel panel_2 = new JPanel();

	/**
	 * Create the panel.
	 */
	public Update_Member_Card_Secondary() {
		panel_1.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel_1.setBackground(SystemColor.inactiveCaption);
		txtEmail.setBounds(114, 210, 133, 20);
		panel_2.setBorder(new LineBorder(SystemColor.control, 6));
		panel_2.setBackground(new Color(220, 220, 220));
		panel_2.add(txtEmail);
		txtEmail.setColumns(10);
		txtPhone.setBounds(114, 168, 133, 20);
		panel_2.add(txtPhone);
		txtPhone.setColumns(10);
		txtID.setBounds(114, 36, 133, 20);
		panel_2.add(txtID);
		txtID.setEditable(false);
		txtID.setColumns(10);
		txtLastName.setBounds(114, 123, 133, 20);
		panel_2.add(txtLastName);
		txtLastName.setColumns(10);
		txtName.setBounds(114, 81, 133, 20);
		panel_2.add(txtName);
		txtName.setColumns(10);

		initGUI();
	}
	private void initGUI() {
		setLayout(null);
		lblUserDetailsUpdate.setForeground(new Color(25, 25, 112));
		lblUserDetailsUpdate.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblUserDetailsUpdate.setBounds(54, 11, 457, 36);
		
		add(lblUserDetailsUpdate);
		panel.setBounds(64, 560, 750, 59);
		
		add(panel);
		panel.setLayout(null);
		btnBack.setBounds(24, 11, 81, 37);
		panel.add(btnBack);
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		btnApply.setBounds(614, 11, 81, 36);
		panel.add(btnApply);
		btnApply.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.setBounds(32, 58, 761, 485);
		
		add(panel_1);
		panel_1.setLayout(null);
		panel_2.setBounds(139, 51, 472, 376);
		
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		lblId.setBounds(20, 37, 46, 14);
		panel_2.add(lblId);
		lblId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFirstName.setBounds(20, 82, 84, 14);
		panel_2.add(lblFirstName);
		lblFirstName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLastName.setBounds(20, 126, 95, 17);
		panel_2.add(lblLastName);
		lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPhone.setBounds(20, 169, 46, 14);
		panel_2.add(lblPhone);
		lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEmail.setBounds(20, 211, 46, 14);
		panel_2.add(lblEmail);
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		txtPassword = new JTextField();
		txtPassword.setBounds(114, 256, 133, 20);
		panel_2.add(txtPassword);
		txtPassword.setColumns(10);
		comboBox.setBounds(114, 303, 133, 20);
		panel_2.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"active", "freeze", "lock", "auto freeze"}));
		
		JLabel lblPasword = new JLabel("Password:");
		lblPasword.setBounds(20, 257, 69, 14);
		panel_2.add(lblPasword);
		lblPasword.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStatus.setBounds(20, 304, 46, 14);
		panel_2.add(lblStatus);
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 14));
		/**
		 * return to the screen check id
		 */
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			Update_Member_Card_Primary up_memmber_card_pri=new  Update_Member_Card_Primary();
				
			on_screen_unload();
			BusinessImplements.GetInstance().getMainFrame().setContentPane(up_memmber_card_pri);
			up_memmber_card_pri.setSize(getMaximumSize());
			up_memmber_card_pri.on_screen_load();
				
				
				
				
			}
		});
		/**
		 * being done when the user press apply
		 * send to the server all the parameters and update the member card 
		 * pop up message if the update successes or not
		 */
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			ArrayList<String>	res=BusinessImplements.GetInstance().getServerProxy().UpdateReaderCard(txtID.getText(),Integer.toString((comboBox.getSelectedIndex())),txtName.getText(),txtLastName.getText(),txtPhone.getText(),txtEmail.getText(),txtPassword.getText());
			if (res.get(0).equalsIgnoreCase("true"))
					JOptionPane.showMessageDialog(null,"The update success");
				else
					JOptionPane.showMessageDialog(null,"Error");
			}
		});
	}
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * when the screen load check if the privilage is: user
	 * didn't applay the option to update status
	 */
	@Override
	public void on_screen_load() {
		if(BusinessImplements.GetInstance().getUserLogin().getUserPosition()==1)
		{
			String iD=Integer.toString(BusinessImplements.GetInstance().getUserLogin().getUserID());
			String password=BusinessImplements.GetInstance().getUserLogin().getPassword();
			Integer role=BusinessImplements.GetInstance().getUserLogin().getUserPosition();
			Member_Card_Controler mem=new Member_Card_Controler();
			Member_DAL member=mem.checkID(Integer.toString(BusinessImplements.GetInstance().getUserLogin().getUserID()));
			setData(member,iD,password);
			comboBox.setEnabled(false);
		}
	}
	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @param mem - details of the member card
	 * @param id - id of the user
	 * @param password - password of the user
	 * the function set the data on the screen
	 * if the privilege : librarian - don't privilege the accesses to update the password
	 */
	public void setData(Member_DAL mem,String id,String password) {
		txtID.setText(id);
		txtName.setText(mem.getUserName());
		txtLastName.setText(mem.getUserLastName());
		txtPhone.setText(mem.getPhoneNumber());
		txtEmail.setText(mem.getEmail());
		String s=mem.getStatus();
		comboBox.setSelectedIndex(Integer.parseInt(s));
		txtPassword.setText(password);
		if (BusinessImplements.GetInstance().getUserLogin().getUserPosition()>=2)
			txtPassword.setEnabled(false);
	}
}
